<?php
require_once('common.inc.php');
require_once(_INC_PATH.'database.php');
require_once(_INC_PATH.'captcha.inc');

if(!checkIsLogin())header("Location: /login.php");
$smarty->assign("myActive",'info');

if($_REQUEST['cmd']=="edit"){
	$db = new database();
	$sql="select id, user_name, email, password, faceImg from tbl_user 
		  where status='0' and id='".mysql_escape_string(_getcookie("id"))."' limit 1";
	$db->setQuery($sql);
	$data = $db->loadRow();	
	setHiddenParam($smarty,$data);
	Display('myinfo_edit.tpl');
}else if($_REQUEST['cmd']=="ajaxCheckPassword"){
	echo checkRegistPassword();
}else if($_REQUEST['cmd']=="ajaxCheckPasswordAgain"){
	echo checkRegistPasswordAgain();
}else if($_REQUEST['cmd']=="confirm"){
	$db = new database();
	$systemMsg = checkEdit();
	if(empty($systemMsg)){
		if($_REQUEST["password"]!=$_REQUEST["hid_password"]){
			
			$sql="update tbl_user set password='".md5(mysql_escape_string($_REQUEST['user_name']))."',
			update_time=CURRENT_TIMESTAMP 
			where id='".mysql_escape_string(_getcookie("id"))."'";
			$db->setQuery($sql);
			$db->query();
		}
		if(!empty($_FILES['faceImg']))
		{
			$srcFile = $_FILES['faceImg'];
			$destFile = array('width'=>'48','height'=>'48','img_name'=>'face','upload_path'=>_IMG_PATH.'/userface/'._getcookie("id").'/');
			uploadImage($srcFile,$destFile);
		}
		$smarty->assign("msg","修改成功！");
		showMyPageList();
	}else{
		setHiddenParam($smarty,$_REQUEST);
		$smarty->assign('systemMsg',$systemMsg);
		Display('myinfo_edit.tpl');
	}
}else{
	showMyPageList();
}
function checkEdit()
{
	$systemMsg=array();
	$ret = checkRegistPassword();
	if($ret != "1")$systemMsg['password'] = $ret;
	$ret = checkRegistPasswordAgain();
	if($ret != "1")$systemMsg['password_again'] = $ret;
	return $systemMsg;
}
function showMyPageList()
{
	global $smarty;
	$db = new database();
	$sql="select id, user_name, email, password,faceImg from tbl_user where status='0' and id='".mysql_escape_string(_getcookie("id"))."' limit 1";
	$db->setQuery($sql);
	$data = $db->loadRow();
	setHiddenParam($smarty,$data);
	Display('myinfo.tpl');
}
function setHiddenParam(&$smarty,$data){
	$smarty->assign("user_name",$data["user_name"]);
	$smarty->assign("email",$data["email"]);
	$smarty->assign("password",$data["password"]);
	$smarty->assign("password_again",(isset($data["password_again"]))?$_REQUEST['password_again']:$data["password"]);
	if(findImg($data['id'],"face")){
		$smarty->assign("faceImg",findImg($data['id'],"face"));
	}else{
		$smarty->assign("faceImg","/images/noface.gif");
	}
}
function checkRegistPassword(){
	if($_REQUEST['password']==""){
		return PASSWORDERROR1;
	}elseif(strlen($_REQUEST['password'])>32||strlen($_REQUEST['password'])<6){
		return PASSWORDERROR2;
	}else{
		return '1';
	}
}
function checkRegistPasswordAgain(){
	if($_REQUEST['password_again']==""){
		return PASSWORDAGAINERROR1;
	}elseif($_REQUEST['password']!=$_REQUEST['password_again']){
		return PASSWORDAGAINERROR2;
	}else{
		return '1';
	}
}
?>